package org.example.bigwork.service.impl;

import org.example.bigwork.entity.Attachment;
import org.example.bigwork.mapper.AttachmentMapper;
import org.example.bigwork.service.AttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AttachmentServiceImpl implements AttachmentService {

    @Autowired
    private AttachmentMapper attachmentMapper;

    @Override
    @Transactional
    public boolean addAttachment(Attachment attachment) {
        // 设置上传时间
        if (attachment.getUploadTime() == null) {
            attachment.setUploadTime(LocalDateTime.now());
        }
        
        try {
            attachmentMapper.insert(attachment);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean deleteAttachment(Integer attachmentId) {
        try {
            attachmentMapper.delete(attachmentId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public List<Attachment> getAttachmentsByTaskId(String taskId) {
        return attachmentMapper.findByTaskId(taskId);
    }
} 